//
//  CZCommentViewController.swift
//  GZWeibo666
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

class CZCommentViewController: UIViewController {
    //接收模型
    var statue : CZStatus?
    //类型
    var controllerType : String?
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置标题
        if controllerType == CZbottomViewBtnType.bottomViewCommentBtn.rawValue {
            self.navigationItem.title = "评论"
        }else{
            self.navigationItem.title = "转发"
        }
       prepareUI()
    }

    func prepareUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendClick")
        
        
        self.view.addSubview(textView)
        textView.backgroundColor = UIColor.orangeColor()
        
        textView.becomeFirstResponder()
//        textView.placeholder = "分享新鲜事..."
        
    
    }
    //MARK: - 点击取消按钮
    func cancelClick() {
        //退出编辑
        view.endEditing(true)
        dismissViewControllerAnimated(true, completion: nil)
    }
    //MARK: - 点击发送按钮
    func sendClick() {
        if controllerType == CZbottomViewBtnType.bottomViewCommentBtn.rawValue {
            //评论功能
            if let text = textView.text {
                CZStatus.sendComment(text, id: (statue?.id)!, finshed: { (result, error) -> () in
                    if error != nil || result == nil {
                        print("\(error)")
                        self.cancelClick()
                        
                    }
                    //有数据
                    SVProgressHUD.showSuccessWithStatus("评论成功!", maskType: SVProgressHUDMaskType.Black)
                    
                    self.cancelClick()
                })
            }
        }else{
            //转发
            CZStatus.forwardStatus((statue?.id)!, text: textView.text, finshed: { (result, error) -> () in
                if error != nil || result == nil {
                    print("\(error)")
                    SVProgressHUD.showErrorWithStatus("转发出错", maskType: SVProgressHUDMaskType.Black)
                    self.cancelClick()
                    
                }
                //有数据
                SVProgressHUD.showSuccessWithStatus("转发成功!", maskType: SVProgressHUDMaskType.Black)
               
                self.cancelClick()
            })
        }
        
    }
    //懒加载
    private lazy var textView: UITextView = UITextView(frame: self.view.bounds)
    
}
